import { createRouter, createWebHashHistory } from 'vue-router'
import { useVersion } from '@/utils/hooks/useVersion'

let redirect = '/real-name'

const constantRoutes = [
  { path: '', redirect },
  { path: '/real-name', component: () => import('@/views/real-name/index.vue'), meta: { title: '实名制认证' } },
  { path: '/intent', component: () => import('@/views/intent/index.vue'), meta: { title: '签署意愿认证' } },
  { path: '/sign', component: () => import('@/views/sign/index.vue'), meta: { title: '手写签名' } },
  { path: '/:pathMatch(.*)*', redirect },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    return new Promise((r) => {
      setTimeout(() => {
        r({ top: 0 })
      }, 200)
    })
  },
})

router.beforeEach((to, from, next) => {
  next()
})

router.onError((error) => {
  console.log(error, 'router.onError')
  let { toJudgeVersionUpdate } = useVersion()
  toJudgeVersionUpdate()
})

export default router

export function setupRouter(app) {
  app.use(router)
}
